package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.SchoolDto;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.forlove.app.qselect.login.service.ISetUserInfoService;
import com.forlove.app.qselect.login.service.impl.SetUserInfoServiceImpl;
import com.forlove.app.qselect.login.view.SetUserInfoView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/27.
 */

public class SetUserInfoPresenter extends IBasePresenter {
    private SetUserInfoView setUserInfoView;
    private ISetUserInfoService setUserInfoService;

    public SetUserInfoPresenter(SetUserInfoView setUserInfoView) {
        this.setUserInfoView = setUserInfoView;
        if (setUserInfoService == null) {
            setUserInfoService = new SetUserInfoServiceImpl();
            setService(setUserInfoService);
        }
    }


    public void loadUpdateUserinfoData(final UserEntity userEntity, String invitID) {
        Map param = new HashMap();
        param.put("realname", userEntity.realname);
        param.put("sex", userEntity.mSex);
        param.put("scholar", userEntity.scholar);
        if (userEntity.issingle > 0) {
            param.put("issingle", userEntity.issingle);
        }
        param.put("address", userEntity.address + "");
        param.put("birthday", userEntity.birthday + "");
        param.put("school", userEntity.school + "");
        param.put("yuanxi", userEntity.yuanxi + "");
        if (StringUtils.isNotEmpty(invitID)) {
            param.put("username_inv", invitID);
        }
        setUserInfoService.loadData(ServerManager.ACTION_UPDATE_USER_INFO, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "设置用户信息:" + result);
                userEntity.parseJSONString(ParseHelper.getString(result, "data.userInfo"));
                if (setUserInfoView != null) {
                    setUserInfoView.updateUserinfoView(userEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (setUserInfoView != null) {
                        setUserInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (setUserInfoView != null) {
                        setUserInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public void upLoadPic(Map param) {
        setUserInfoService.uploadFile(ServerManager.ACTION_UPLOAD_IMAGE, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "上传头像:" + result);
                String photoUrl = ParseHelper.getString(result, "data.img_name");
                if (setUserInfoView != null) {
                    setUserInfoView.updateSetPhotoView(photoUrl);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "上传头像 onFail:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    setUserInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    setUserInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }


    public void loadSchoolInfo() {
        setUserInfoService.loadData(ServerManager.ACTION_GET_SCHOOL, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "学校信息:" + result);
                String school = ParseHelper.getString(result, "data");
                if (StringUtils.isNotEmpty(school)) {
                    SchoolDto schoolDto = JSON.parseObject(school, SchoolDto.class);
                    if (schoolDto != null) {
                        DataManager.saveSchoolInfo(school);
                    }
                    setUserInfoView.updateSchoolInfo(schoolDto.getData());
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    setUserInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    setUserInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }
}
